package socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * URL 
 * 放在 TCP UDP 后讲
 *
 */
public class Test2 {

	public static void main(String[] args) throws Exception {
		
		URL baidu = new  URL("http://www.baidu.com");
		URL url=new URL(baidu, "/index.html?username=tom");
		
		System.out.println("协议"+url.getProtocol());
		System.out.println("主机"+url.getHost());
		System.out.println("端口"+url.getPort());
		
		System.out.println("路径"+url.getPath());	
		System.out.println("文件"+url.getFile());
		System.out.println("查询参数"+url.getQuery());
		
		//用URL读取文件
		InputStream is= baidu.openStream();
		InputStreamReader isr=new InputStreamReader(is,"utf-8");
		BufferedReader br= new BufferedReader(isr);
		String data=br.readLine();
		while(data!=null){
			System.out.println(data);
			data=br.readLine();
		}
		br.close();
		isr.close();
		is.close();

	}

}
